package cn.wolfcode.p2p.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;

    /**
     * 创建充值账户流水模板
     */
    public void createBaseFlow(Account account, BigDecimal amount, int actionType, String note) {
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setActionType(actionType);
        accountFlow.setAccountId(account.getId());
        accountFlow.setAmount(amount);
        accountFlow.setNote(note);
        accountFlow.setUsableAmount(account.getUsableAmount());
        accountFlow.setFreezedAmount(account.getFreezedAmount());
        accountFlow.setActionTime(new Date());
        accountFlowMapper.insert(accountFlow);
    }

    @Override
    public void createAccountFlow(Account account, BigDecimal amount) {
        createBaseFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE, "线下充值:" + amount);
    }

    @Override
    public void createBidFreezedFlow(Account account, BigDecimal amount, String bidRequestTitle) {
        createBaseFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED,
                "投标" + bidRequestTitle + "可用余额减少,冻结金额增加" + amount);
    }

    @Override
    public void createAccountErrorFlow(Account bidAccount, BigDecimal availableAmount, String bidRequestTitle) {
        createBaseFlow(bidAccount, availableAmount, Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED,
                "取消投标" + bidRequestTitle + "冻结金额减少,可用余额增加" + availableAmount);
    }

    @Override
    public void createBidSuccessFlow(Account bidAccount, BigDecimal availableAmount, String title) {
        createBaseFlow(bidAccount, availableAmount, Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL,
                "成功投标" + title + "冻结金额减少" + availableAmount);
    }

    @Override
    public void createBorrowSuccessFlow(Account borrowUser, BigDecimal bidRequestAmount, String title) {
        createBaseFlow(borrowUser, bidRequestAmount, Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL,
                "二审成功" + title + "收到借款金额" + bidRequestAmount);
    }

    @Override
    public void createPayAccountManagementChargeFlow(Account borrowUser, BigDecimal accountManagementCharge, String title) {
        createBaseFlow(borrowUser, accountManagementCharge, Constants.ACCOUNT_ACTIONTYPE_CHARGE,
                "成功借款,支付平台手续费" + title + "借款金额减少" + accountManagementCharge);
    }

    @Override
    public void createReturnMoneySuccessFlow(Account borrowAccount, BigDecimal returnAmount, String bidRequestTitle) {
        createBaseFlow(borrowAccount, returnAmount, Constants.ACCOUNT_ACTIONTYPE_RETURN_MONEY,
                "还款成功" + bidRequestTitle + "可用金额减少" + returnAmount);
    }

    @Override
    public void createReceiveMonetSuccessFlow(Account bidAccount, BigDecimal totalAmount, PaymentSchedule ps) {
        createBaseFlow(bidAccount, totalAmount, Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY,
                "[" + ps.getBidRequestTitle() + "]第" + ps.getMonthIndex() + "期,收款成功,可用余额增加" + totalAmount);
    }

    @Override
    public void createPayInterestManageCharge(Account bidAccount, BigDecimal interestManagerCharge, PaymentSchedule ps) {
        createBaseFlow(bidAccount, interestManagerCharge, Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE,
                "[" + ps.getBidRequestTitle() + "]第" + ps.getMonthIndex() + "期,支付平台利息管理费,可用余额减少" + interestManagerCharge);
    }

    @Override
    public void createTransferSuccessFlow(Account transferFromAccount, BigDecimal bidRequestAmount, String bidRequestTitle) {
        createBaseFlow(transferFromAccount, bidRequestAmount, Constants.ACCOUNT_ACTIONTYPE_TRANSFER_SUCCESS,
                 bidRequestTitle + "债权标转让成功,可用余额增加" + bidRequestAmount);

    }

    @Override
    public void createSubscribeSuccessFlow(Account transferToAccount, BigDecimal bidRequestAmount, String bidRequestTitle) {
        createBaseFlow(transferToAccount, bidRequestAmount, Constants.ACCOUNT_ACTIONTYPE_SUBSCRIPTION_SUCCESS,
                bidRequestTitle + "认购债权标成功,可用余额减少" + bidRequestAmount);
    }

    @Override
    public void createRechargeOnlineFlow(Account account, BigDecimal amount) {
        createBaseFlow(account, amount, Constants.ACCOUNT_ONLINE_RECHARGE_SUCCESS,
                 "线上充值成功,可用余额增加" + amount);
    }
}
